package pt.record.model;

import design.patterns.factory.IFactory;
import model.FavoritableEntity;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import static pt.record.model.Record.TEAMS;

/**
 * Created by nuno-rafael-figueiredo on 15-03-2014.
 */
public final class Matches extends FavoritableEntity<Integer, Match> implements IFactory<Match, Element> {

    public Matches() {
        super(Match[].class, Match.class);
    }

    @Override
    public Match create(Element contentItem) {
        Match match = new Match()
                .setHomeTeam(TEAMS.get(contentItem.select(".liveScoreBox1ContentTextLeft").get(0).text()))
                .setGuestTeam(TEAMS.get(contentItem.select(".liveScoreBox1ContentTextRight").get(0).text()))
                .setHomeGoals(contentItem.select(".liveScorePontosLeft").get(0).text())
                .setGuestGoals(contentItem.select(".liveScorePontosRight").get(0).text());

        Elements elements = contentItem.select("img");
        if (elements.size() > 0)
            match.setTv(elements.get(0).attr("alt"));

        if (containsValue(match))
            return get(match.hashCode()).update(match);

        put(match.hashCode(), match);

        return match;
    }

    @Override
    protected void parse() {
    }
}
